package com.banciyuan.bcywebview.biz.detail.goods;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.base.view.tag.FlowView;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.goods.shoppingcart.ShoppingCartActivity;
import com.banciyuan.bcywebview.biz.goods.shoppingcart.ShoppingCartHelper;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.biz.share.ShareActivity;
import com.banciyuan.bcywebview.biz.web.WebActivity;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.currency.CurrencyUtil;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.regex.MatchFilterImpl;
import com.banciyuan.bcywebview.utils.regex.MyLinkfy;
import com.banciyuan.bcywebview.utils.regex.MyRegex;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.DetailGoods;
import de.greenrobot.daoexample.model.Multi;
import de.greenrobot.daoexample.model.PhoneShell;
import de.greenrobot.daoexample.model.TagDetail;
import de.greenrobot.daoexample.model.Timeline;


/**
 * Created by squall on 2016/10/18.
 */

public class GoodsDetailActivity extends BaseActivity implements View.OnClickListener, View.OnCreateContextMenuListener {

    private static final int GO_SELECT = 1477;
    private static final int GO_CAR = 1478;

    private ListView mListView;
    private TextView goods_name;
    private ImageView back_iv;
    private ImageView share_iv;
    private FrameLayout goods_car;
    private View goods_car_num;

    private ImageView praise_iv;
    private TextView praise_tv;
    private ImageView recommend_iv;
    private TextView recommend_tv;

    private TextView add_cars;
    private TextView buy_now;
    private View lockView;
    private TextView lockReason;
    private TextView select_type;
    private TextView work_flow;
    private FlowView tag_flow;
    private LinearLayout user_say_header;
    private TextView lock_helper;
    private View viewAuthorOtherGoods;
    private View viewProduceOtherGoods;

    private ViewPager mViewPager;
    private LinearLayout banner_indicator;
    private TextView goods_title;
    private TextView goods_price;
    private LinearLayout work_line;
    private LinearLayout tag_line;
    private ImageView user_header;
    private TextView user_name;
    private TextView user_intro;
    private TextView user_focus;
    private TextView user_says;
    private RelativeLayout category;
    private GoodsHelper goodsHelper;

    private ProgressbarHelper mProgressbarHelper;
    private View mProgressbarView;

    private GoodsDetailAdapter detailAdapter;
    private DetailGoods detailGoods = new DetailGoods();
    private List<DetailGoods> dlist = new ArrayList<>();
    private List<Fragment> flist = new ArrayList<>();
    protected String zg_id;

    private boolean isAnimationRunning;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_activity);
        initArgs();
        initProgressbar();
        initUi();
        initData();
    }

    @Override
    protected void initArgs() {
        zg_id = getIntent().getStringExtra(gotoUtil.INTENT_VALUE_ONE);
        goodsHelper = new GoodsHelper(this);
        dlist = getBuyCar();
    }

    @Override
    protected void initUi() {
        mListView = (ListView) findViewById(R.id.main_rv);
        goods_name = (TextView) findViewById(R.id.base_action_bar_title);
        back_iv = (ImageView) findViewById(R.id.base_action_bar_home);
        share_iv = (ImageView) findViewById(R.id.goods_share);
        goods_car = (FrameLayout) findViewById(R.id.goods_car);
        goods_car_num = findViewById(R.id.buy_car_num);
        praise_iv = (ImageView) findViewById(R.id.iv_detail_praise);
        praise_tv = (TextView) findViewById(R.id.tv_detail_praise);
        recommend_iv = (ImageView) findViewById(R.id.iv_detail_recommend);
        recommend_tv = (TextView) findViewById(R.id.tv_detail_recommend);
        add_cars = (TextView) findViewById(R.id.add_to_cars);
        buy_now = (TextView) findViewById(R.id.buy_now);
        lockView = findViewById(R.id.ll_detail_locked);
        lockReason = (TextView) findViewById(R.id.lock_reason);
        lock_helper = (TextView) findViewById(R.id.tv_post_lock_link);

        View header = View.inflate(this, R.layout.goods_header_item, null);
        select_type = (TextView) header.findViewById(R.id.select_type);
        mViewPager = (ViewPager) header.findViewById(R.id.goods_banner);
        banner_indicator = (LinearLayout) header.findViewById(R.id.banner_indicator);
        goods_title = (TextView) header.findViewById(R.id.goods_title);
        goods_price = (TextView) header.findViewById(R.id.goods_price);
        work_line = (LinearLayout) header.findViewById(R.id.work_line);
        tag_line = (LinearLayout) header.findViewById(R.id.tag_line);
        user_header = (ImageView) header.findViewById(R.id.goods_user_header);
        user_focus = (TextView) header.findViewById(R.id.goods_user_focus);
        user_name = (TextView) header.findViewById(R.id.user_name);
        user_intro = (TextView) header.findViewById(R.id.user_intro);
        user_says = (TextView) header.findViewById(R.id.user_says);
        category = (RelativeLayout) header.findViewById(R.id.category_container);
        work_flow = (TextView) header.findViewById(R.id.work_line_flow);
        tag_flow = (FlowView) header.findViewById(R.id.tag_line_flow);
        user_say_header = (LinearLayout) header.findViewById(R.id.user_say_header);
        viewAuthorOtherGoods = header.findViewById(R.id.author_other_goods);
        viewProduceOtherGoods = header.findViewById(R.id.product_other_goods);

        mListView.addHeaderView(header);
        View footerblank = new View(this);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, UIUtils.dip2px(50, this));
        footerblank.setLayoutParams(params);
        footerblank.setBackgroundColor(getResources().getColor(R.color.white));
        mListView.addFooterView(footerblank);
    }

    @Override
    protected void initAction() {
        category.setOnClickListener(this);
        back_iv.setOnClickListener(this);
        user_header.setOnClickListener(this);
        user_name.setOnClickListener(this);
        goods_car.setOnClickListener(this);
        share_iv.setOnClickListener(this);
        user_focus.setOnClickListener(this);
        buy_now.setOnClickListener(this);
        add_cars.setOnClickListener(this);
        work_flow.setOnClickListener(this);
        findViewById(R.id.view_detail_praise).setOnClickListener(this);
        findViewById(R.id.view_detail_recommend).setOnClickListener(this);

        user_says.setOnCreateContextMenuListener(this);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < banner_indicator.getChildCount(); i++) {
                    if (position == i) {
                        banner_indicator.getChildAt(i).setBackgroundResource(R.drawable.shape_circle_alpha90_pink);
                    } else {
                        banner_indicator.getChildAt(i).setBackgroundResource(R.drawable.shape_circle_alpha10_gray);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initProgressbar() {
        mProgressbarView = findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new ProgressbarHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                mProgressbarHelper.onLoading();
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initData() {
        goodsHelper.getGoodsDetail(zg_id, new GoodsHelperInter() {
            @Override
            public void onSucc(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    Gson gson = new Gson();
                    detailGoods = gson.fromJson(jsonObject.getString(HttpUtils.UPLOAD_DATA), DetailGoods.class);
                    detailGoods.setCount(1);
                    sortPhoneShell();
                    if (detailGoods.getLock() != null) {
                        renderLock();
                    } else {
                        renderData();
                        initAction();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(String fail) {
                try {
                    JSONObject jsonObject = new JSONObject(fail);
                    int status = jsonObject.getInt(HttpUtils.UPLOAD_STATUS);
                    switch (status) {
                        case MyConstants.NOTEXIT:
                        case MyConstants.DATA_NOTEXIT:
                            mProgressbarHelper.onFailed(getString(R.string.content_not_exist));
                            break;
                        case MyConstants.NEED_LOGIN:
                            mProgressbarHelper.onFailed(getString(R.string.loginfirst));
                            break;
                        case MyConstants.SOMETINGS_WRONG:
                            mProgressbarHelper.onFailed();
                            break;
                        case MyConstants.PERSON_NOT_EXIST:
                            mProgressbarHelper.onFailed();
                            break;
                        case MyConstants.WORK_LOKED:
                            renderLock();
                            break;
                    }
                } catch (JSONException e) {
                    mProgressbarHelper.onFailed();
                }
            }
        });
    }

    private void renderLock() {
        lockView.setVisibility(View.VISIBLE);
        if (detailGoods.getLock() != null) {
            if (TextUtils.isEmpty(detailGoods.getLock().getReason())) {
                lockReason.setText(Html.fromHtml(String.format(getString(R.string.goods_locked_one), getString(R.string.goods_locked_default))));
            } else {
                lockReason.setText(Html.fromHtml(String.format(getString(R.string.goods_locked_one), detailGoods.getLock().getReason())));
            }
        } else {
            lockReason.setText(Html.fromHtml(String.format(getString(R.string.goods_locked_one), getString(R.string.goods_locked_default))));
        }
        lock_helper.setOnClickListener(this);
        mProgressbarHelper.onSuccess();
    }

    private void renderData() {
        renderHeader();
        if (detailAdapter == null) {
            if (detailGoods.getTech_arts() == null) {
                detailGoods.setTech_arts(new ArrayList<Multi>());
            } else {
                detailAdapter = new GoodsDetailAdapter(detailGoods.getTech_arts(), this);
            }
        } else {
            detailAdapter.notifyDataSetChanged();
        }
        mListView.setAdapter(detailAdapter);
        mProgressbarHelper.onSuccess();
    }

    private void renderHeader() {
        for (int i = 0; i < detailGoods.getMulti().size(); i++) {
            View dot = View.inflate(this, R.layout.goods_img_dot, null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(UIUtils.dip2px(7, this), UIUtils.dip2px(7, this));
            params.leftMargin = UIUtils.dip2px(9, this);
            dot.setLayoutParams(params);
            if (i > 0) {
                dot.setBackgroundResource(R.drawable.shape_circle_alpha10_gray);
            }
            banner_indicator.addView(dot);
            GoodsPicFragment gf = new GoodsPicFragment();
            Bundle bundle = new Bundle();
            bundle.putSerializable("images", (Serializable) detailGoods.getMulti());
            bundle.putInt("index", i);
            gf.setArguments(bundle);
            flist.add(gf);
        }
        if (!TextUtils.isEmpty(detailGoods.getTitle())) {
            goods_name.setText(detailGoods.getTitle());
            goods_title.setText(detailGoods.getTitle());
        }
        if (!TextUtils.isEmpty(detailGoods.getBasic_price())) {
            goods_price.setText(String.format(getString(R.string.price_format), CurrencyUtil.getMoney(detailGoods.getBasic_price())));
        }
        if (!TextUtils.isEmpty(detailGoods.getWork())) {
            if (!TextUtils.isEmpty(detailGoods.getWid())) {
                work_flow.setText(detailGoods.getWork());
            } else {
                work_flow.setText(Html.fromHtml(String.format(getString(R.string.goods_work_canot_click), detailGoods.getWork())));
            }
        } else {
            work_line.setVisibility(View.GONE);
        }
        if (!detailGoods.getPost_tags().isEmpty()) {
            tag_flow.setVisibility(View.VISIBLE);
            tag_flow.removeAllViews();
            for (TagDetail t : detailGoods.getPost_tags()) {
                addText("tag", t.getTag_name());
            }
        } else {
            tag_line.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(detailGoods.getProfile().getAvatar())) {
            ImageLoader.getInstance().displayImage(detailGoods.getProfile().getAvatar(), user_header, ImageOptions.optionHead);
        }
        if (!TextUtils.isEmpty(detailGoods.getProfile().getSelf_intro())) {
            user_intro.setText(Html.fromHtml(detailGoods.getProfile().getSelf_intro()));
        }
        if (!TextUtils.isEmpty(detailGoods.getProfile().getUname())) {
            user_name.setText(detailGoods.getProfile().getUname());
        }
        if (!StringUtil.notNullEqual(detailGoods.getProfile().getFollowstate(), "unfollow") || StringUtil.notNullEqual(UserDataHelper.getInstance(this).getUid(), detailGoods.getUid())) {
            user_focus.setVisibility(View.GONE);
        } else {
            user_focus.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(detailGoods.getIntro())) {
            user_says.setText(Html.fromHtml(detailGoods.getIntro()));
            user_say_header.setVisibility(View.VISIBLE);
            user_says.setVisibility(View.VISIBLE);
            MyLinkfy.addLinks(user_says, MyRegex.plist.get(0), "bcyapp_", new MatchFilterImpl(), null);
        } else {
            user_say_header.setVisibility(View.GONE);
            user_says.setVisibility(View.GONE);
        }
        if (detailGoods.isHave_ding()) {
            praise_iv.setImageResource(R.drawable.detail_praise_done);
        } else {
            praise_iv.setImageResource(R.drawable.detail_praise);
        }
        if (detailGoods.getDing_num() == 0) {
            praise_tv.setText(getString(R.string.good));
        } else {
            praise_tv.setText(String.valueOf(detailGoods.getDing_num()));
        }
        if (detailGoods.isHave_tuijian()) {
            recommend_iv.setImageResource(R.drawable.detail_recommend_done);
            recommend_tv.setText(getString(R.string.all_recommended));
        } else {
            recommend_iv.setImageResource(R.drawable.detail_recommend);
            recommend_tv.setText(getString(R.string.all_recommend));
        }
        if (dlist.size() > 0) {
            goods_car_num.setVisibility(View.VISIBLE);
        } else {
            goods_car_num.setVisibility(View.GONE);
        }

        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return flist.get(position);
            }

            @Override
            public int getCount() {
                return detailGoods.getMulti().size();
            }
        });

        if (null == detailGoods.getAuthor_other_goods() || detailGoods.getAuthor_other_goods().isEmpty()) {
            viewAuthorOtherGoods.setVisibility(View.GONE);
        } else {
            viewAuthorOtherGoods.setVisibility(View.VISIBLE);
            ((TextView) viewProduceOtherGoods.findViewById(R.id.goods_recommend_title)).setText(getString(R.string.other_goods_by_author));
            dealRecommendGoods(viewAuthorOtherGoods, detailGoods.getAuthor_other_goods());
            viewAuthorOtherGoods.findViewById(R.id.othergoods_container).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(GoodsDetailActivity.this, SmoothPersonActivity.class, detailGoods.getUid());
                }
            });
        }

        if (null == detailGoods.getProduct_other_goods() || detailGoods.getProduct_other_goods().isEmpty()) {
            viewProduceOtherGoods.setVisibility(View.GONE);
        } else {
            viewProduceOtherGoods.setVisibility(View.VISIBLE);
            viewProduceOtherGoods.findViewById(R.id.more_iv).setVisibility(View.GONE);
            viewProduceOtherGoods.findViewById(R.id.more_tv).setVisibility(View.GONE);
            ((TextView) viewProduceOtherGoods.findViewById(R.id.goods_recommend_title)).setText(getString(R.string.others_products_goods));
            dealRecommendGoods(viewProduceOtherGoods, detailGoods.getProduct_other_goods());
        }
    }

    private void dealRecommendGoods(View rootView, List<DetailGoods> detailGoodsList) {
        LinearLayout llGoodsContainer = (LinearLayout) rootView.findViewById(R.id.ll_goods_container);
        llGoodsContainer.removeAllViews();
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        int lineCount = detailGoodsList.size() % 3 == 0 ? detailGoodsList.size() / 3 : detailGoodsList.size() / 3 + 1;
        for (int i = 0; i < lineCount; i++) {
            View goodsLineView = layoutInflater.inflate(R.layout.goods_recommend_item, llGoodsContainer, false);
            ImageView[] goodsPics = new ImageView[]{(ImageView) goodsLineView.findViewById(R.id.goods_pic_one),
                    (ImageView) goodsLineView.findViewById(R.id.goods_pic_two), (ImageView) goodsLineView.findViewById(R.id.goods_pic_three)};

            TextView[] goodsTitles = new TextView[]{(TextView) goodsLineView.findViewById(R.id.goods_title_one),
                    (TextView) goodsLineView.findViewById(R.id.goods_title_two), (TextView) goodsLineView.findViewById(R.id.goods_title_three)};

            TextView[] goodsPrices = new TextView[]{(TextView) goodsLineView.findViewById(R.id.goods_price_one),
                    (TextView) goodsLineView.findViewById(R.id.goods_price_two), (TextView) goodsLineView.findViewById(R.id.goods_price_three)};
            View[] goodsView = new View[]{goodsLineView.findViewById(R.id.goods_info_one), goodsLineView.findViewById(R.id.goods_info_two), goodsLineView.findViewById(R.id.goods_info_three)};
            int innerGoodsCount = i < lineCount - 1 || detailGoodsList.size() % 3 == 0 ? 3 : detailGoodsList.size() % 3;
            for (int j = 0; j < innerGoodsCount; j++) {
                final DetailGoods detailGoods = detailGoodsList.get(i * 3 + j);
                ImageLoader.getInstance().displayImage(detailGoods.getCover(), goodsPics[j], ImageOptions.options);
                goodsTitles[j].setText(detailGoods.getTitle());
                goodsPrices[j].setText(String.format(getString(R.string.price_format), CurrencyUtil.getMoney(detailGoods.getBasic_price())));
                goodsView[j].setVisibility(View.VISIBLE);
                goodsView[j].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        gotoUtil.gotoAct(GoodsDetailActivity.this, GoodsDetailActivity.class, detailGoods.getZg_id());
                    }
                });
            }
            llGoodsContainer.addView(goodsLineView);
        }

    }

    private void doPostDing(final boolean ding) {
        if (isAnimationRunning) return;
        goodsHelper.postDing(zg_id, new GoodsHelperInter() {
            @Override
            public void onSucc(String response) {
                detailGoods.setHave_ding(ding);
                if (detailGoods.isHave_ding()) {
                    isAnimationRunning = true;
                    detailGoods.setDing_num(detailGoods.getDing_num() + 1);

                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(UIUtils.dip2px(22, GoodsDetailActivity.this), UIUtils.dip2px(22, GoodsDetailActivity.this));
                    praise_iv.setLayoutParams(layoutParams);
                    praise_iv.setScaleX(2f);
                    praise_iv.setScaleY(2f);

                    praise_iv.setImageResource(R.drawable.frame_praise);
                    AnimationDrawable drawable = (AnimationDrawable) praise_iv.getDrawable();
                    drawable.start();
                    praise_iv.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            praise_iv.setScaleX(1f);
                            praise_iv.setScaleY(1f);

                            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(UIUtils.dip2px(44, GoodsDetailActivity.this), LinearLayout.LayoutParams.WRAP_CONTENT);
                            praise_iv.setLayoutParams(layoutParams);

                            praise_iv.setImageResource(R.drawable.detail_praise_done);
                            praise_tv.setText(String.valueOf(detailGoods.getDing_num()));
                            isAnimationRunning = false;
                        }
                    }, 20 * 24);

                } else {
                    detailGoods.setDing_num(detailGoods.getDing_num() - 1);
                    praise_iv.setImageResource(R.drawable.detail_praise);
                    praise_tv.setText(String.valueOf(detailGoods.getDing_num()));
                    if (detailGoods.getDing_num() == 0) {
                        praise_tv.setText(getString(R.string.good));
                    }
                }
            }

            @Override
            public void onFail(String fail) {

            }
        }, ding);
    }

    private void doPostTuijian(final boolean tuijian) {
        goodsHelper.postTuijian(zg_id, new GoodsHelperInter() {
            @Override
            public void onSucc(String response) {
                detailGoods.setHave_tuijian(tuijian);
                if (detailGoods.isHave_tuijian()) {
                    recommend_iv.setImageResource(R.drawable.detail_recommend_done);
                    recommend_tv.setText(getString(R.string.all_recommended));
                } else {
                    recommend_iv.setImageResource(R.drawable.detail_recommend);
                    recommend_tv.setText(getString(R.string.all_recommend));
                }
            }

            @Override
            public void onFail(String fail) {
            }
        }, tuijian);
    }

    private void goShare() {
        try {
            Timeline timeline = new Timeline();
            timeline.setUid(detailGoods.getUid());
            timeline.setZg_id(detailGoods.getZg_id());
            timeline.setContentAddress(detailGoods.getCover());
            timeline.setoUName(detailGoods.getProfile().getUname());
            timeline.setTitle(detailGoods.getTitle());
            timeline.setTimelineType(TimelineFactory.TIMELINE_TYPE_GOODS);

            Intent intent = new Intent(this, ShareActivity.class);
            intent.putExtra("showblock", false);
            intent.putExtra("showDelBtn", false);
            intent.putExtra("showWarnBtn", true);
            intent.putExtra("showCopyLink", true);
            intent.putExtra("showShare", true);
            intent.putExtra("is_zhipin", true);
            intent.putExtra("timeline", timeline);

            startActivity(intent);
            overridePendingTransition(R.anim.umeng_socialize_fade_in, R.anim.umeng_socialize_fade_out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void userFocus() {
        TimelineLikeHelper.doFollow(this, "dofollow", detailGoods.getUid(), new TimelineLikeHelper.FollowSuccess() {
            @Override
            public void onFollowSuccess(String response) {
                if (StatusCodeUtils.dealStatusCode(response, GoodsDetailActivity.this)) {
                    MyToast.show(GoodsDetailActivity.this, getString(R.string.focus_succ));
                    user_focus.setVisibility(View.GONE);
                }
            }
        });
    }

    private List<DetailGoods> getBuyCar() {
        return ShoppingCartHelper.getShoppingItems(this);
    }

    private void addText(final String type, final String name) {
        TextView textView = new TextView(this);
        textView.setText(name);
        textView.setTextSize(12);
        textView.setTextColor(getResources().getColorStateList(R.color.timeline_green));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.leftMargin = UIUtils.dip2px(12, this);
        textView.setLayoutParams(params);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.equals("work")) {
                    if (!TextUtils.isEmpty(detailGoods.getWid())) {
                        gotoUtil.gotoAct(GoodsDetailActivity.this, CircleSmoothActivity.class, "", detailGoods.getWid());
                    }
                } else {
                    gotoUtil.gotoAct(GoodsDetailActivity.this, CircleTagSmoothActivity.class, name, detailGoods.getWid());
                }
            }
        });
        tag_flow.addView(textView);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.base_action_bar_home:
                finish();
                break;
            case R.id.goods_share:
                goShare();
                break;
            case R.id.category_container:
                goSelect("all");
                break;
            case R.id.view_detail_praise:
                doPostDing(!detailGoods.isHave_ding());
                break;
            case R.id.view_detail_recommend:
                doPostTuijian(!detailGoods.isHave_tuijian());
                break;
            case R.id.add_to_cars:
                goSelect("add");
                break;
            case R.id.buy_now:
                goSelect("buy");
                break;
            case R.id.goods_car:
                intent = new Intent(this, ShoppingCartActivity.class);
                startActivityForResult(intent, GO_CAR);
                break;
            case R.id.goods_user_focus:
                userFocus();
                break;
            case R.id.work_line_flow:
                if (!TextUtils.isEmpty(detailGoods.getWid())) {
                    gotoUtil.gotoAct(this, CircleSmoothActivity.class, "", detailGoods.getWid());
                }
                break;
            case R.id.goods_user_header:
            case R.id.user_name:
                gotoUtil.gotoAct(this, SmoothPersonActivity.class, detailGoods.getUid());
                break;
            case R.id.tv_post_lock_link:
                gotoUtil.gotoAct(this, WebActivity.class, getString(R.string.zhipin_helper));
                break;
        }
    }

    private void goSelect(String type) {
        Intent intent = new Intent(GoodsDetailActivity.this, GoodsSelectActivity.class);
        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, detailGoods);
        intent.putExtra(gotoUtil.INTENT_VALUE_TWO, type);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, GO_SELECT);
        overridePendingTransition(R.anim.umeng_socialize_fade_in, R.anim.umeng_socialize_fade_out);
    }

    private void refreashBuyCar() {
        if (dlist.size() > 0) {
            goods_car_num.setVisibility(View.VISIBLE);
        } else {
            goods_car_num.setVisibility(View.GONE);
        }
    }

    private void renderType() {
        select_type.setText(String.format(getString(R.string.has_select), detailGoods.getSelect_sku().getDescription()));
    }

    private void sortPhoneShell() {
        List<PhoneShell> phoneShells = new ArrayList<>();
        for (DetailGoods.Sku_list s : detailGoods.getSku_data()) {
            for (PhoneShell p : detailGoods.getPhone_shells()) {
                if (s.getDescription().equals(p.getDescription())) {
                    phoneShells.add(p);
                }
            }
        }
        detailGoods.setPhone_shells(phoneShells);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (requestCode == GO_SELECT) {
                detailGoods = (DetailGoods) data.getSerializableExtra(gotoUtil.INTENT_VALUE_ONE);
                dlist = ShoppingCartHelper.getShoppingItems(this);
                refreashBuyCar();
                renderType();
            } else if (requestCode == GO_CAR) {
                dlist = ShoppingCartHelper.getShoppingItems(this);
                refreashBuyCar();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        try {
            menu.add(0, v.getId(), 0, getString(R.string.copy_paste));
            TextView textView = (TextView) v;
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("shareText", textView.getText().toString());
            clipboard.setPrimaryClip(clipData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}